<?php 
use yii\helpers\Html;
use yii\helpers\Url;
use leandrogehlen\treegrid\TreeGrid;
use yii\widgets\Pjax;

$this->title = Yii::t('app', '菜单管理');
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="news-index box box-primary">
	<?php Pjax::begin(); ?>
    <div class="box-header with-border">
        <?= Html::a(Yii::t('app', '新建顶级菜单'),null, ['class' => 'btn btn-success btn-flat category-create-top']) ?>
    </div>
    <div class="box-body table-responsive no-padding">

<?= TreeGrid::widget([
    'dataProvider' => $dataProvider,
    'keyColumnName' => 'id',
    'parentColumnName' => 'pid',
    'parentRootValue' => '0', //first parentId value
    'pluginOptions' => [
        // 'initialState' => 'collapsed',
    ],
    'columns' => [
        'title',
        'sort',
        // 'pid',

        //操作按钮
        [
            'options' => ['width' => '135px'],
            'format'  => 'raw',
            'label'   => Yii::t('app', '操作'),
            'value'   => function ($model) {
                    $actionDom = Html::a(
                        Html::tag('span', '',['class' => 'glyphicon glyphicon-plus category-create-child']),
                        null,
                        // Url::to(['create', 'parent_id' => $model['id']]),
                        [
                            'title' => '新增子菜单',
                            'aria-label'=>"View",
                            'data'  => [
                                'pjax' => 0,     
                            ],
                            'cate-id'=>$model['id'],
                        ]
                    );
                
                    $actionDom .= '  ';
                    $actionDom .= Html::a(
                        Html::tag('span', '',['class' => 'glyphicon glyphicon-pencil category-update']),
                        null,
                        // Url::to(['update', 'id' => $model['id']]),
                        [
                            'title' => '更新菜单信息',
                            'aria-label'=>"Update",
                            'data'  => [
                                'pjax' => 0,
                            ],
                            'cate-id'=>$model['id'],
                        ]
                    );

                    $actionDom .= '  ';
                    $actionDom .= Html::a(
                        Html::tag('span', '',['class' => 'glyphicon glyphicon-trash']),
                        Url::to(['delete', 'id' => $model['id']]),
                        [
                            'title' => '删除菜单',
                            'aria-label'=>"Delete",
                            'data'  => [
                                'confirm' => Yii::t('app', '确定删除吗？'),
                                'pjax' => 0,
                                'method' => 'post'
                            ],
                        ]
                    );
                    return $actionDom;
            },
        ],
    ]     
]); ?>
<?php Pjax::end(); ?>

</div>
</div>

<?php //按需注入js, 放在</body>前面
$this->registerJs("
		$(function(){
			$(document).on('click', '.category-create-top ,.category-create-child ,.category-update', function(event) {
				var title ='';
				var url ='';
				if($(this).is('.category-create-top')){
					title = $(this).text();
					url = '/menu/create';
				}else if($(this).is('.category-create-child')){
					title = '创建子级菜单';
					url = '/menu/create';
				}else if($(this).is('.category-update')){
					title = '更新菜单信息';
					url = '/menu/update';
				}
				var id = $(this).parent('a').attr('cate-id');
				if(typeof id !=='undefined'){
					url +='?id='+id;
				}

				layer.open({
				    type: 2,
				    anim: 0,
	                shade: [0.3, '#393D49'],
	                // skin: 'layui-layer-molv',
	                title: title,
	                skin: 'layui-layer-rim', //加上边框
	                area: ['1200', '650px'], //自定义文本域宽高
				    content: url //注意，如果str是object，那么需要字符拼接。
				});
			});
		})

	", \yii\web\View::POS_END);
 ?>